package com.ny.zuoye2567.dao.dao;

import com.ny.zuoye2567.dao.DbUntil;
import com.ny.zuoye2567.dao.shiti.Product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao{
    DbUntil dbUntil = new DbUntil();

    public List<Product> findAllProduct() throws SQLException,ClassNotFoundException
    {
        Connection connection = dbUntil.getCon();
        String sql = "select * from products";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Product> productList = new ArrayList<Product>();
        while (resultSet.next()){
            Product product = new Product();
            int id = resultSet.getInt("product_id");
            product.setProductId(id);
            product.setProductName(resultSet.getString("name"));
            product.setPrice(resultSet.getDouble("price"));
            product.setStock(resultSet.getInt("stock"));
            product.setCategory(resultSet.getString("category"));
            product.setDescription(resultSet.getString("description"));
            productList.add(product);
        }
        return productList;
    }

    @Override
    public int insertProduct(Product newProduct) {
        return 0;
    }

    @Override
    public int deleteProduct(String delName) {
        return 0;
    }

    @Override
    public Product findByName(String oldName) {
        return null;
    }

    @Override
    public int updateProduct(Product productToUpdate) {
        return 0;
    }


}
